package com.gserver.game.tank.handler;

import java.util.Map;

import com.gserver.commons.ByteArray;
import com.gserver.commons.Randam;
import com.gserver.game.GameRunTimeException;
import com.gserver.game.ServerMainHandler;
import com.gserver.game.character.Player;
import com.gserver.game.domain.OnlinePlayer;
import com.gserver.netty.domain.Message;

public class TankHandler extends ServerMainHandler {

	@Override
	public void execute(Message message, ByteArray byteArray)
			throws GameRunTimeException {

		// ByteArray s = new ByteArray(message.getData());
		short command = message.getCmd();
		Player player = message.getPlayer();
		logger.info("client sendMessage:" + command);
		Map<Integer, Player> onlinePlayers;
		switch (command) {
		case (short) 1:
			player = new Player();
			player.setPassportId(Randam.getInt());
			int pos = player.getPassportId() * 10;
			player.setChannel(message.getChannel());
			OnlinePlayer.addOnlinePlayer(player);

			onlinePlayers = OnlinePlayer.getOnlinePlayers();
			for (Map.Entry<Integer, Player> e : onlinePlayers.entrySet()) {
				Player value = e.getValue();
				value.sendMessage(message.getHandler(), (short) 10,
						player.getPassportId(), pos, pos);
			}

			break;
		case (short) 2:
			short x = byteArray.readShort();
			short y = byteArray.readShort();

			Player findPlayer = OnlinePlayer.findPlayer(player.getPassportId());
			if (findPlayer != null) {

				onlinePlayers = OnlinePlayer.getOnlinePlayers();
				for (Map.Entry<Integer, Player> e : onlinePlayers.entrySet()) {
					player = e.getValue();
					if (player.getPassportId() == player.getPassportId()) {
						continue;
					}
					player.sendMessage(message.getHandler(), (short) 11,
							player.getPassportId(), x, y);
				}
			}
			break;

		default:
			break;
		}
		logger.info("client data:" + byteArray.readInt());

	}
}
